//
//  VisitorView.swift
//  Weibo
//
//  Created by huangjunyang on 15/10/4.
//  Copyright © 2015年 hjy. All rights reserved.
//

import UIKit

 /// 设置代理

protocol VisitorViewDelegate:NSObjectProtocol {
    
    func visitorViewWillregister()
    
    func visitorViewWillLogin()
    
}


class VisitorView: UIView {

  // MARK - 界面初始化
    
    
    //定义代理
    
    weak var delegate: VisitorViewDelegate?
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
        
    }
    
    //提供接口给外界 设置视图信息
    
    func setupViewInfo(ishome: Bool ,imaName: String , labtitle: String) {
        
        messageLable.text = labtitle
        iconView.image = UIImage(named: imaName)
        
        homeIconView.hidden = !ishome
        
        ishome ? startAnimation() : sendSubviewToBack(maskIconView)
        
        
    }
    
    
    //注册按钮的点击事件
    
    func clickRegister() {
        
        delegate?.visitorViewWillregister()
        
    }
    
    //登陆按钮的点击事件
    
    func  clickLogin() {
        
        delegate?.visitorViewWillLogin()
        
    }

    private func startAnimation() {
        
        // 设置核心动画
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20.0
        
        //核心动画结束后不移除
        anim.removedOnCompletion = false
        
        //添加核心动画
        iconView.layer.addAnimation(anim, forKey: nil)
        
        
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        
         super.init(coder: aDecoder)
        

        setupUI()

    }
    
    // MARK - 设置界面
    
    private func setupUI() {
        
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(homeIconView)
        addSubview(messageLable)
        addSubview(registerButton)
        addSubview(logingButton)
        
 ///设置自动布局约束
      
        //1.图标的约束
        iconView.translatesAutoresizingMaskIntoConstraints = false
        
        //添加约束 multiplier 乘数 constant 一个常量值
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -30))
        
        //2.小房子
        
        homeIconView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        //3,lable 
        
        messageLable.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item:messageLable , attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        
         addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.Top, relatedBy:NSLayoutRelation.Equal, toItem: iconView, attribute:NSLayoutAttribute.Bottom , multiplier: 1.0, constant:16 ))
        
        //设置宽度
        
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
        
        
        //添加注册按钮
    
    registerButton.translatesAutoresizingMaskIntoConstraints = false
     
     addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        
    addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        
    addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        
    addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        
        
    //添加登陆按钮
        
    logingButton.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: logingButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: logingButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: logingButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        
        addConstraint(NSLayoutConstraint(item: logingButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        

        
        //添加遮罩视图约束
        
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        
    addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview" : maskIconView]))
    addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subview]-(-35)-[regbutton]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview" : maskIconView,"regbutton":registerButton]))
        
       //设置背景颜色
        
      backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
        
    
        
        
    }
    
    //MARK - 懒加载子控件
    
    /// 图标
    private lazy var iconView: UIImageView = {
       let iv = UIImageView(image: UIImage(named:"visitordiscover_feed_image_smallicon" ))
        
        return iv
    }()
    

    ///  遮罩视图
    
    private lazy var maskIconView: UIImageView = {
       let iv = UIImageView(image:UIImage(named:"visitordiscover_feed_mask_smallicon" ) )
        
        return iv
        
    }()
    
    /// 小房子
    private lazy var homeIconView: UIImageView = {
       
        let iv = UIImageView(image: UIImage(named:"visitordiscover_feed_image_house"))
        
        return iv
    
    }()
    
    /// 消息文字
    
    private lazy var messageLable: UILabel = {
        
       let lb = UILabel()
        
        lb.text = "关注一些人，回这里看看有什么惊喜"
        lb.textColor = UIColor.darkGrayColor()
        lb.numberOfLines = 0
        
        lb.font = UIFont.systemFontOfSize(14)
        lb.sizeToFit()
        
        return lb
    }()
    
    /// 注册按钮
    
    private lazy var registerButton: UIButton = {
       
        let btn = UIButton()
        
        btn.setTitle("注册", forState:UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState:UIControlState.Normal)
        
        
        //增加一个点击事件
         btn.addTarget(self, action: "clickRegister", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        return btn
    }()
    
    ///登陆按钮
    
    private lazy var logingButton: UIButton = {
       let btn = UIButton()

        btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        btn.setTitle("登陆", forState:UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "clickLogin", forControlEvents: UIControlEvents.TouchUpInside)
        

        
        return btn
        
    }()
    
    
}













